package com.peakmain.wanandroid.loginmodule.presenter;

import android.annotation.SuppressLint;

import com.peakmain.wanandroid.loginmodule.contract.LoginContract;
import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;
import com.peakmain.wanandroid.netmodule.bean.DataResponse;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author: peakmain
 * createdata：2019/7/10
 * mail: 2726449200@qq.com
 * desiption:
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter{
    @Inject
    public LoginPresenter(){

    }
    @SuppressLint("CheckResult")
    @Override
    public void Login(String account, String password) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).login(account,password)
                .compose(mView.<DataResponse>bindToLife())
                .compose(RxSchedulers.<DataResponse>applySchedulers())
                .subscribe(new Consumer<DataResponse>() {
                    @Override
                    public void accept(DataResponse dataResponse) throws Exception {
                           if(dataResponse.getErrorCode()==0){
                               mView.showLoginSuccess();
                           }else{
                               mView.showFaild(dataResponse.getErrorMessage());
                           }
                           mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                           mView.showFaild("网络错误，请稍后重试");
                           mView.hideLoading();
                    }
                });
    }
}
